package com.xinruke.hostels.reservation.interfaces.api;

import com.xinruke.hostels.common.fallback.FeignFallback;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.reservation.interfaces.dto.*;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author GuoZheng
 * @ClassName ReservationFallback
 * @date：2019/6/22
 * @version: V1.0.0
 * @description：预订熔断
 */
@Slf4j
@Component
public class ReservationFallback implements FallbackFactory<ReservationApi> {
    @Resource
    private FeignFallback feignFallback;

    @Override
    public ReservationApi create(Throwable throwable) {
        log.error("系统异常：", throwable);

        return new ReservationApi() {

            @Override
            public ResultVO<BigDecimal> ifCanDiscounts(@Valid DiscountsIfCanQueryDTO params) {
                return feignFallback.doFallback(ReservationFallback.class, "ifCanDiscounts", "是否可以优惠出错");
            }

            @Override
            public ResultVO checkIn(@Valid List<DiscountsCheckInQueryDTO> params) {
                return feignFallback.doFallback(ReservationFallback.class, "checkIn", "入住优惠出错");
            }

            @Override
            public ResultVO makeUp(@Valid List<DiscountsMakeUpQueryDTO> params) {
                return feignFallback.doFallback(ReservationFallback.class, "makeUp", "补偿优惠出错");
            }

            @Override
            public ResultVO addDiscounts(@Valid DiscountsAddQueryDTO params) {
                return feignFallback.doFallback(ReservationFallback.class, "add", "初始化优惠出错");
            }

            @Override
            public ResultVO<Integer> remaining(@Valid DiscountsRemainingQueryDTO params) {
                return feignFallback.doFallback(ReservationFallback.class, "remaining", "查询剩余优惠出错");

            }

            @Override
            public ResultVO<RowsDataVO<DiscountsRecordQueryResultDTO>> record(@Valid DiscountsRecordQueryDTO params) {
                return feignFallback.doFallback(ReservationFallback.class, "record", "查询优惠出错");

            }

        };
    }
}

